var express = require('express');
const sqlQuery = require('../../model/HGmysql');
var router = express.Router();

/* GET article page. */
router.get('/article', async(req, res, next) => {
    try {
        //获取前台传参数据
        let { currentPage, pageSize } = req.query

       let strSql = `SELECT id,title,content,class_name01,class_name02,class_name03,type,pic_url,visited,like_count,DATE_FORMAT(create_time,'%Y-%m-%d') AS create_time FROM article where type = 0 limit ${(currentPage - 1) * pageSize},${pageSize}`
       let result = await sqlQuery(strSql)
            //查询数据多少条
       let strSql2 = "select count(id) as title from article"
       let result1 = await sqlQuery(strSql2)
      let  count = result1[0].title
        res.json({
            state: 200,
            count,
            result
        })

    } catch (error) {
        console.log(error);
        next()
    }
});

router.get('/Lunbo', async(req, res, next) => {
        let strSql = `SELECT id,title,content,class_name01,class_name02,class_name03,type,pic_url,like_count,DATE_FORMAT(create_time,'%Y-%m-%d') AS create_time FROM Lunbo`
        let result = await sqlQuery(strSql)
        res.json({
            state: 200,
            result
        })
    })
    //文章详情
router.get('/detail', async(req, res) => {
        let article_id = req.query.article_id
        try {
            let strSql = `select id,title,content,
    class_name01,classify_id01,
    class_name02,classify_id02,
    class_name03,classify_id03,
    visited,like_count,pic_url,
    DATE_FORMAT(create_time,'%Y-%m-%d') AS create_time from article where id = ?`
            let result = await sqlQuery(strSql, article_id)
            let visited = result[0].visited + 1
            console.log("访问量", visited);
            // 将文章访问量＋1后更新到文章中
            await sqlQuery('update article set visited = ? where id = ?', [visited, article_id])
            res.json({
                state: 200,
                data: result[0]
            })
        } catch (error) {
            console.log(error);
            res.json({
                state: 500,
                data: '获取错误'
            })
        }
    })
    //轮播图文章详情
router.get('/detailLun', async(req, res) => {
    let article_id = req.query.article_id
    try {
        let strSql = `select id,title,content,
    class_name01,classify_id01,
    class_name02,classify_id02,
    class_name03,classify_id03,
    visited,like_count,pic_url,
    DATE_FORMAT(create_time,'%Y-%m-%d') AS create_time from lunbo where id = ?`
        let result = await sqlQuery(strSql, article_id)
        let visited = result[0].visited + 1
        console.log("访问量", visited);
        // 将文章访问量＋1后更新到文章中
        await sqlQuery('update article set visited = ? where id = ?', [visited, article_id])
        res.json({
            state: 200,
            data: result[0]
        })
    } catch (error) {
        console.log(error);
        res.json({
            state: 500,
            data: '获取错误'
        })
    }
})
router.post('/like', async(req, res) => {
    let { article_id } = req.body
    let { username } = req.user
    try {

        let sql = 'select like_count from article where id = ? '
        let result = await sqlQuery(sql, [article_id])
        let newlikeCount = result[0].like_count + 1;

        let likesql = 'update article set like_count = ? where id = ?'
        await sqlQuery(likesql, [newlikeCount, article_id])

        let likeres = await sqlQuery('select like_count from article where id = ?', [article_id])

        res.send({ code: 0, msg: '点赞成功', data: likeres[0] })
    } catch (error) {
        console.log(error);
    }
})
router.post('/likeLun', async(req, res) => {
        let { article_id } = req.body
        let { username } = req.user
        try {
            let sql = 'select like_count from lunbo where id = ? '
            let result = await sqlQuery(sql, [article_id])
            let newlikeCount = result[0].like_count + 1;

            let likesql = 'update lunbo set like_count = ? where id = ?'
            await sqlQuery(likesql, [newlikeCount, article_id])
            let likeres = await sqlQuery('select like_count from lunbo where id = ?', [article_id])
            res.send({ code: 0, msg: '点赞成功', data: likeres[0] })
        } catch (error) {
            console.log(error);
        }
    })
    /* 发布评论接口 */
router.post('/publish', async(req, res, next) => {
    let { content, article_id } = req.body;
    // 获取当前用户名称
    let { username } = req.user
    try {
        // 根据用户名称查用户的id,头像和昵称
        let user = await sqlQuery('select id,head_img,nickname from user where username = ?', [username]);
        let { id: user_id, head_img, nickname } = user[0]

        let sql = 'insert into comment(user_id,article_id,cmcontent,nickname,head_img,create_time) values(?,?,?,?,?,NOW())'
        await sqlQuery(sql, [user_id, article_id, content, nickname, head_img])
        res.send({ code: 0, msg: '发表成功', data: null })
    } catch (e) {
        console.log(e);
        // 把错误交给错误中间件处理
        next(e)
    }
});
//全部留言接口
router.get('/Allmessages', async(req, res) => {
        let { article_id } = req.query
        try {
            let sql = 'select id,head_img,nickname,cmcontent,link_conut,DATE_FORMAT(create_time,"%Y-%m-%d %H:%i:%s") AS create_time from comment where article_id = ?'
            let result = await sqlQuery(sql, [article_id])
            res.send({ code: 0, msg: '成功', data: result })
        } catch (error) {}
    })
    //管理员删除评论
router.post('/delete', async(req, res, next) => {
    let { comment_id } = req.body
    try {
        let sql = 'delete from comment where id = ?'
        let result = await sqlQuery(sql, [comment_id])
        res.send({ code: 0, msg: '删除成功', data: null })
    } catch (e) {
        console.log(e)
        next(e)
    }
});
//评论点赞接口
router.post('/clickLike', async(req, res) => {
        //解析前台传参
        console.log(req.body);
        let { id } = req.body
        try {
            let sql = 'select link_conut from comment where id = ? '
            let result = await sqlQuery(sql, [id])
            let newlikeCount = result[0].link_conut + 1;
            let likesql = 'update comment set link_conut = ? where id = ?'
            await sqlQuery(likesql, [newlikeCount, id])
            let likeres = await sqlQuery('select link_conut from comment where id = ?', [id])
            res.send({ code: 0, msg: '点赞成功', data: likeres[0] })
        } catch (error) {
            console.log(error);
        }
    })
    //轮播图评论
router.post('/publishlun', async(req, res, next) => {
    let { content, article_id } = req.body;
    // 获取当前用户名称
    let { username } = req.user
    try {
        // 根据用户名称查用户的id,头像和昵称
        let user = await sqlQuery('select id,head_img,nickname from user where username = ?', [username]);
        let { id: user_id, head_img, nickname } = user[0]

        let sql = 'insert into comment_lunbo(user_id,article_id,cmcontent,nickname,head_img,create_time) values(?,?,?,?,?,NOW())'
        await sqlQuery(sql, [user_id, article_id, content, nickname, head_img])
        res.send({ code: 0, msg: '发表成功', data: null })
    } catch (e) {
        console.log(e);
        // 把错误交给错误中间件处理
        next(e)
    }
});
router.get('/Allmessageslun', async(req, res) => {
        let { article_id } = req.query
        try {
            let sql = 'select id,head_img,nickname,cmcontent,link_conut,DATE_FORMAT(create_time,"%Y-%m-%d %H:%i:%s") AS create_time from comment_lunbo where article_id = ?'
            let result = await sqlQuery(sql, [article_id])
            res.send({ code: 0, msg: '成功', data: result })
        } catch (error) {}
    })
    //管理员删除评论
router.post('/deletelun', async(req, res, next) => {
    let { comment_id } = req.body
    try {
        let sql = 'delete from comment_lunbo where id = ?'
        let result = await sqlQuery(sql, [comment_id])
        res.send({ code: 0, msg: '删除成功', data: null })
    } catch (e) {
        console.log(e)
        next(e)
    }
});
//评论点赞接口
router.post('/clickLikelun', async(req, res) => {
    //解析前台传参
    console.log(req.body);
    let { id } = req.body
    try {
        let sql = 'select link_conut from comment_lunbo where id = ? '
        let result = await sqlQuery(sql, [id])
        let newlikeCount = result[0].link_conut + 1;
        let likesql = 'update comment_lunbo set link_conut = ? where id = ?'
        await sqlQuery(likesql, [newlikeCount, id])
        let likeres = await sqlQuery('select link_conut from comment_lunbo where id = ?', [id])
        res.send({ code: 0, msg: '点赞成功', data: likeres[0] })
    } catch (error) {
        console.log(error);
    }
})
module.exports = router;